//
//  TVTopicInputView.m
//  LTFM
//
//  Created by MY053 on 14-12-16.
//  Copyright (c) 2014年 yx. All rights reserved.
//

#import "TVTopicInputView.h"
#import <QuartzCore/QuartzCore.h>
#import "MBProgressHUD.h"
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@implementation TVTopicInputView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor colorWithRed:6/255.0 green:157/255.0 blue:214/255.0 alpha:1]];
        //添加输入框
        [self addTextFieldView];
    }
    return self;
}

- (void)addTextFieldView
{
     UIImage *img = [[UIImage imageNamed:@"input_background"] stretchableImageWithLeftCapWidth:12 topCapHeight:12];
    bkImageView = [[UIImageView alloc]initWithImage:img];
    bkImageView.frame=CGRectMake(20, 5, self.frame.size.width - 70, 30);
    [self addSubview:bkImageView];
    
    _tfView = [[UITextField alloc] init];
    [_tfView setFrame:CGRectMake(30, 5, self.frame.size.width - 85, 30)];
    [_tfView setText:@""];
    _tfView.returnKeyType = UIReturnKeyDone;
    [_tfView setBorderStyle:UITextBorderStyleNone];
    [_tfView setPlaceholder:@"回复在这里"];
    [_tfView setFont:[UIFont systemFontOfSize:15]];
    _tfView.delegate = self;
    [self addSubview:_tfView];
    
    //评论
    sendButton = [[UIButton alloc] init];
    [sendButton setFrame:CGRectMake(self.frame.size.width-40, 3, 33, 33)];
    [sendButton setImage:[UIImage imageNamed:@"btn_comment_1"] forState:UIControlStateNormal];
    [sendButton addTarget:self action:@selector(clickSendButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:sendButton];
    
    //注册键盘检测
    [self registerForKeyboardNotifications];
    
}
- (void)registerForKeyboardNotifications
{
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    
    //[[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void) keyboardWasHidden:(NSNotification *) notif
{
    
//    [UIView animateWithDuration:0.3f animations:^{
//        [self.delegate.view setCenter:CGPointMake(self.delegate.view.center.x, self.delegate.view.frame.size.height / 2)];
//    }];
}
- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect beginKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    CGFloat yOffset = endKeyboardRect.origin.y - beginKeyboardRect.origin.y;
    NSLog(@"%f",yOffset);
    [UIView animateWithDuration:duration animations:^{
        if (endKeyboardRect.origin.y==ScreenHeight) {
             [self.delegate.view setCenter:CGPointMake(self.center.x, ScreenHeight/2)];
        }
        else
        {
     [self.delegate.view setCenter:CGPointMake(self.center.x, ScreenHeight/2-endKeyboardRect.size.height)];
        }
    }];
}

- (void) keyboardWasShown:(NSNotification *) notif
{
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setRecord
{
    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError *sessionError;
    //    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
    //    [session setCategory: AVAudioSessionCategorySoloAmbient error: nil];
    [session setMode:AVAudioSessionModeVideoRecording error:&sessionError];
    if(session == nil)
    {
        NSLog(@"Error creating session: %@", [sessionError description]);
    }else
    {
        [session setActive:YES error:nil];
    }
}

//发送test
- (void)clickSendButton:(UIButton *)btn
{
    //文字
    [_tfView resignFirstResponder];
    NSString *comment =  [_tfView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if(comment.length == 0)
    {
        MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.delegate.view];
        hud.labelText = @"回复不能为空";
        [self.delegate.view addSubview:hud];
        [hud show:YES];
        [hud hide:YES afterDelay:1.0f];
    }else
    {
        [self sendText:comment];
        _tfView.text = @"";
    }
}

- (void)sendText:(NSString *)text
{
    if([self.delegate respondsToSelector:@selector(sendTextMessage:)])
    {
        [self.delegate sendTextMessage:text];
    }
}

#pragma mark text field delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
         [textField resignFirstResponder];
        return YES;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
